<!DOCTYPE html>
<html>
  <head>
    <title>
      scriptprocessornode-detached-no-crash.html
    </title>
    <script src="../../resources/js-test.js"></script>
    <script src="../resources/audit-util.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      description(
          'Tests document-detached use of AudioContext, pass if no crash.');

      window.jsTestIsAsync = true;

      let sampleRate = 44100.0;
      let renderLengthInFrames = 512;
      let bufferSize = 512;
      let context;
      let node;
      function runTest() {
        try {
          node = context.createScriptProcessor(bufferSize, 0, 1);
          let source = context.createBufferSource();
          source.buffer = createImpulseBuffer(context, bufferSize);
          node.onaudioprocess = function(e) {};
          source.connect(node);
          node.connect(context.destination);
          source.start(0);

          context.startRendering();
        } catch (e) {
          // The context has been stopped and detached; nothing to test.
          return;
        }
      }

      let w;
      function processMessage(event) {
        if (event.data == 'opened') {
          context =
              new w.OfflineAudioContext(1, renderLengthInFrames, sampleRate);
          w.close();
        } else if (event.data == 'closed') {
          runTest();
          finishJSTest();
        }
      }

      if (window.testRunner) {
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
        testRunner.setPopupBlockingEnabled(false);
      }

      w = window.open('../../resources/window-postmessage-open-close.html');
      window.addEventListener('message', processMessage, false);
    </script>
  </body>
</html>
